<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import {useRouter} from "vue-router";
import {useStore} from "vuex";
import api from "@/api/index.js";

const router = useRouter();
const store = useStore();
const username = ref('');
const password = ref('');

const handleLogin = () => {
  // 处理登录逻辑
  api.login({
    username: username.value,
    password: password.value
  }).then(resp => {
    console.log(resp.data)
    // user, token, perms
    store.commit('SET_AUTH',resp.data)
    api.setToken(resp.data.token)
    console.log('登录信息:', username.value, password.value);
    ElMessage.success('登录成功！');
    setTimeout(()=>{
      router.push('/')
    })


  }).catch(e => {
    console.log(e)
    ElMessage.error("登录失败")
  })


};
</script>

<template>
  <div class="login-container">

    <transition name="fade">
      <el-card class="login-form">
        <h2>登录</h2>
        <el-form @submit.prevent="handleLogin">
          <el-form-item label="用户名">
            <el-input v-model="username" placeholder="请输入用户名" required />
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="password" type="password" placeholder="请输入密码" required />
          </el-form-item>
          <el-form-item>
            <el-button type="success" native-type="submit" style="width: 100%;">登录</el-button>
          </el-form-item>
          <el-link type="primary"  @click="router.push('/register')">注册</el-link>
        </el-form>
      </el-card>
    </transition>
    <div style="padding: 50px">
      <h1>欢迎使用学习资源推荐平台</h1>
    </div>
  </div>
</template>

<style scoped>
.login-container {

  display: flex;
  justify-content: center;
  align-items: center;
  height: 80vh;
}

.login-form {
  background: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  width: 300px;
}

.form-group {
  margin-bottom: 1rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
}

input {
  width: 100%;
  padding: 0.5rem;
  border: 1px solid #ccc;
  border-radius: 4px;
}

button {
  width: 100%;
  padding: 0.5rem;
  background-color: #a2dd1b;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #89b50d;
}

.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}

.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>